package myself.a008CodingTest;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/26
 * Time 9:55
 * Mail 739153436@qq.com
 */
public class FindFirstIntersectNode {
    // 先计算出链表是否有环，如果有环，以两个环的交点作为参数代入对应的方法
    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        // 遍历判断是否有环
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        // 如果有环，进行环的交点的获取
        if (loop1 == null && loop2 == null) { // 无环
            return noLoop(head1, head2);
        } else if (loop1 != null && loop2 != null) { // 两个环
            return bothLoop(head1, loop1, head2, loop2);
        } else { // 一个有环
            return null;
        }
    }

    public static Node getLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        // n1 慢  n2 快
        Node slow = head.next; // n1 -> slow
        Node fast = head.next.next; // n2 -> fast
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        // slow fast  相遇
        fast = head; // n2 -> walk again from head
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    // 两种情况：无环、有环
    public static Node noLoop(Node head1, Node head2) {
        // 判断是否有交点
        Node cur1 = head1;
        Node cur2 = head2;
        int count = 0;
        while (cur1.next != null) {
            count++;
            cur1 = cur1.next;
        }
        while (cur2.next != null) {
            count--;
            cur2 = cur2.next;
        }
        if (cur1 != cur2) {
            return null;
        }
        // 如果有交点，那么求交点
        cur1 = count >= 0 ? head1 : head2;
        cur2 = count >= 0 ? head2 : head1;
        count = Math.abs(count); // 坑，如果为负数需要注意
        while (count > 0) {
            count--;
            cur1 = cur1.next;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        // 情况一：有交点并且交点在环外
        if (loop1 == loop2) {
            // 判断是否有交点
            Node cur1 = head1;
            Node cur2 = head2;
            int count = 0;
            while (cur1 != loop1) {
                count++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {
                count--;
                cur2 = cur2.next;
            }
            // 如果有交点，那么求交点
            cur1 = count >= 0 ? head1 : head2;
            cur2 = count >= 0 ? head2 : head1;
            count = Math.abs(count); // 坑，如果为负数需要注意
            while (count > 0) {
                count--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        }
        // 情况二：有交点或者没有交点
        if (loop1 != loop2) {
            Node stop = loop1;
            loop1 = loop1.next;
            while (loop1 != stop) {
                if (loop1 == loop2) {
                    return loop1;
                }
                loop1 = loop1.next;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        Node head2 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
//        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
//        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }

    // 笔试的话可以使用HashSet来实现
    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

}
